<template>
    <div>
<h2>会员等级添加</h2>
<table class="table table-bordered">
<tbody>
    <tr>
        <td>会员等级名称</td>
        <td><input type="text"v-model="a.LevelName"></td>
    </tr>
    <tr>
        <td>累计消费金额</td>
        <td><input type="text"v-model="a.Money"></td>
    </tr>
    <tr>
        <td>权益</td>
        <td><select multiple size="3" v-model="a.LevelRights">
            <option value="">请选择</option>
            <option :value="item.RightName" v-for="item in data.list">{{item.RightName}}</option>
        </select></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="button" value="添加" class="btn btn-primary" @click="add"/></td>
    </tr>
</tbody>
</table>
    </div>
</template>

<script setup lang="ts">
import{ref,reactive,onMounted}from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router=useRouter();

onMounted(()=>{
    getlist();
})

let data:any=reactive({
    list:[]
})


let a:any=reactive({
    LevelName:'',
    Money:'',
    LevelRights:[]
});

const getlist:any=()=>{
    axios({
        url:'/api/Right/GetRightModels',
        method:'get'
    })
    .then((res)=>{
        data.list=res.data
    })
}

const add=()=>{
    if(a.LevelName=='')
    {
        alert("名称不能为空");
        return;
    }
    a.LevelRights=a.LevelRights.toString();
    axios({
        method:'post',
        url:'/api/Member/Add',
        data:a,
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
        {
            alert("增加成功");
            router.push("/memberlevel");
        }
        if(res.data==-1)
        {
            alert("名称重复");
            return;
        }
    })
    .catch((err) => {
      console.log(err);
    });
}


</script>

<style scoped>

</style>